package com.example.eldersystem.controller;

import com.example.eldersystem.operatedata.model.OldPerson;
import com.example.eldersystem.response.DataResponse;
import com.example.eldersystem.operatedata.model.Event;
import com.example.eldersystem.operatedata.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@RestController
@RequestMapping("/event")
public class EventController {

    @Autowired
    EventService eventService;

    @RequestMapping("/add")
    private DataResponse<Map<Integer,String>> addEvent(@RequestBody Map<String,Object> map) {
        DataResponse<Map<Integer,String>> dataResponse = new DataResponse<>();

        List<LinkedHashMap<String,Object>> listMap= (List<LinkedHashMap<String, Object>>) map.get("list");
        Map<Integer,String> resultMap = new HashMap<>(listMap.size());
        String str ="";
        for (int n = 0;n<listMap.size();n++) {
            Event event = new Event();
            event.setEventType(listMap.get(n).get("eventType").toString());
            Date udate = null;
            String eventDate = listMap.get(n).get("eventDate").toString();
            try {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                format.setTimeZone(TimeZone.getTimeZone("GTM+8"));
                udate = format.parse(eventDate);
            } catch (ParseException e) {
                str = "日期格式错误";
                resultMap.put(n + 1, str);
                break;
            }
            event.setEventDate(udate);
            event.setEventDescription(listMap.get(n).get("eventDescription").toString());
            event.setEventLocation(listMap.get(n).get("eventLocation").toString());
            if (listMap.get(n).get("oldPersonId") != null && listMap.get(n).get("oldPersonId").toString() != "") {
                event.setOldPersonId(Integer.parseInt(listMap.get(n).get("oldPersonId").toString()));
            }
            if (listMap.get(n).get("volunteerId") != null && listMap.get(n).get("volunteerId").toString() != "") {
                event.setVolunteerId(Integer.parseInt(listMap.get(n).get("volunteerId").toString()));
            }
            str = eventService.addEvent(event).getMsg();
            resultMap.put(n+1,str);
        }
        dataResponse.setData(resultMap);
        return dataResponse;
    }

    @RequestMapping("/count")
    private DataResponse<Integer> countEventNum(){
        DataResponse<Integer> dataResponse = new DataResponse<>();
        dataResponse.setData(eventService.getEventNum());
        return dataResponse;
    }

    @RequestMapping("/select")
    private DataResponse<List<Event>> selectEvent(){
        DataResponse<List<Event>> dataResponse;
        dataResponse=eventService.selectEvent();
        return dataResponse;
    }

    @RequestMapping("/searchName")
    private DataResponse<List<Event>> searchEventByName(@RequestBody Map<String,Object> map){
        DataResponse<List<Event>> dataResponse = new DataResponse<>();
        String userName;
        try{
            userName = map.get("userName").toString();
        }catch(Exception e){
            dataResponse.setCode(1);
            dataResponse.setMsg("请求错误");
            return dataResponse;
        }
        dataResponse=eventService.selectEventByName(userName);
        return dataResponse;
    };
}
